/******************************************************************************
秋夜静，独坐对残灯。
啼笑非非谁识我，坐行梦梦尽缘君，何所慰消沉。
风卷雨，雨复卷侬心。
心似欲随风雨去，茫茫大海任浮沉。
无爱亦无恨。
******************************************************************************/
#include "RGB_Slave.h"
#include <stdio.h>
#include <string.h>

static void rgb_slave_test_ack(void)
{
    union RGB_Slave_t slave;
    union RGB_Slave_t deslave;
    struct RGB_Slave_ack ack;
    uint8_t buf[128];
    uint8_t debuf[128];
    int len1,len2, len3;
    slave.head = RGB_SLAVE_HEAD;
    slave.cmd = RGB_SLAVE_CMD_ACK;
    slave.ack._cmd = RGB_SLAVE_CMD_INIT;
    slave.ack.code = 0;
    slave.ack.check = 0x33;
    memset(buf, 0x00, sizeof(buf));
    memset(debuf, 0x00, sizeof(debuf));
    len1 = rgb_slave_encode(buf, &slave);
    len2 = rgb_slave_decode(buf, sizeof(buf), &deslave);
    len3 = rgb_slave_check(buf, sizeof(buf));
    rgb_slave_encode(debuf, &deslave);
    if(0==memcmp(buf, debuf, sizeof(buf))) printf("[%s--%d] Test OK len: %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
    else
    {
        printf("[%s--%d] len:  %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
        printf("[%s--%d] head: 0x%02X 0x%02X \r\n", __func__, __LINE__, slave.head, deslave.head);
    }
    memset(buf, 0x00, sizeof(buf));
    memset(debuf, 0x00, sizeof(debuf));
    len1 = rgb_slave_ack(buf, slave.ack._cmd, slave.ack.code, slave.ack.check);
    len2 = rgb_slave_decode(buf, sizeof(buf), &deslave);
    len3 = rgb_slave_check(buf, sizeof(buf));
    len3 = rgb_slave_ack_decode(buf, sizeof(buf), &ack);
    rgb_slave_encode(debuf, &deslave);
    if(0==memcmp(buf, debuf, sizeof(buf))) printf("[%s--%d] Test OK len: %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
    else
    {
        printf("[%s--%d] len:  %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
        printf("[%s--%d] head: 0x%02X 0x%02X \r\n", __func__, __LINE__, slave.head, deslave.head);
    }
}
static void rgb_slave_test_sleep(void)
{
    union RGB_Slave_t slave;
    union RGB_Slave_t deslave;
    uint8_t buf[128];
    uint8_t debuf[128];
    int len1,len2, len3;
    slave.head = RGB_SLAVE_HEAD;
    slave.cmd = RGB_SLAVE_CMD_SLEEP;
    memset(buf, 0x00, sizeof(buf));
    memset(debuf, 0x00, sizeof(debuf));
    len1 = rgb_slave_encode(buf, &slave);
    len2 = rgb_slave_decode(buf, sizeof(buf), &deslave);
    len3 = rgb_slave_check(buf, sizeof(buf));
    rgb_slave_encode(debuf, &deslave);
    if(0==memcmp(buf, debuf, sizeof(buf))) printf("[%s--%d] Test OK len: %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
    else
    {
        printf("[%s--%d] len:  %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
        printf("[%s--%d] head: 0x%02X 0x%02X \r\n", __func__, __LINE__, slave.head, deslave.head);
    }
    memset(buf, 0x00, sizeof(buf));
    memset(debuf, 0x00, sizeof(debuf));
    len1 = rgb_slave_sleep(buf);
    len2 = rgb_slave_decode(buf, sizeof(buf), &deslave);
    len3 = rgb_slave_check(buf, sizeof(buf));
    rgb_slave_encode(debuf, &deslave);
    if(0==memcmp(buf, debuf, sizeof(buf))) printf("[%s--%d] Test OK len: %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
    else
    {
        printf("[%s--%d] len:  %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
        printf("[%s--%d] head: 0x%02X 0x%02X \r\n", __func__, __LINE__, slave.head, deslave.head);
    }
}
static void rgb_slave_test_init(void)
{
    union RGB_Slave_t slave;
    union RGB_Slave_t deslave;
    struct RGB_Slave_init init;
    uint8_t buf[128];
    uint8_t debuf[128];
    int len1,len2, len3;
    slave.head = RGB_SLAVE_HEAD;
    slave.cmd = RGB_SLAVE_CMD_INIT;
    slave.init.cols = 21;
    slave.init.rows = 6;
    slave.init.IdxLogo = 0xFF;
    slave.init.reverseX = 0x55;
    slave.init.reverseY = 0x00;
    slave.init.masks[2] = 0x67893322;
    slave.init.palettes[3] = 0x44556633;
    memset(buf, 0x00, sizeof(buf));
    memset(debuf, 0x00, sizeof(debuf));
    len1 = rgb_slave_encode(buf, &slave);
    len2 = rgb_slave_decode(buf, sizeof(buf), &deslave);
    len3 = rgb_slave_check(buf, sizeof(buf));
    rgb_slave_encode(debuf, &deslave);
    if(0==memcmp(buf, debuf, sizeof(buf))) printf("[%s--%d] Test OK len: %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
    else
    {
        printf("[%s--%d] len:  %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
        printf("[%s--%d] head: 0x%02X 0x%02X \r\n", __func__, __LINE__, slave.head, deslave.head);
    }
    memset(buf, 0x00, sizeof(buf));
    memset(debuf, 0x00, sizeof(debuf));
    len1 = rgb_slave_init(buf, slave.init.cols, slave.init.rows, slave.init.IdxLogo, 0x55, 0, slave.init.masks, slave.init.palettes);
    len2 = rgb_slave_decode(buf, sizeof(buf), &deslave);
    len3 = rgb_slave_check(buf, sizeof(buf));
    len3 = rgb_slave_init_decode(buf, sizeof(buf), &init);
    rgb_slave_encode(debuf, &deslave);
    if(0==memcmp(buf, debuf, sizeof(buf))) printf("[%s--%d] Test OK len: %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
    else
    {
        printf("[%s--%d] len:  %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
        printf("[%s--%d] head: 0x%02X 0x%02X \r\n", __func__, __LINE__, slave.head, deslave.head);
    }
}
static void rgb_slave_test_ctrl(void)
{
    union RGB_Slave_t slave;
    union RGB_Slave_t deslave;
    struct RGB_Slave_control ctrl;
    uint8_t buf[128];
    uint8_t debuf[128];
    int len1,len2, len3;
    slave.head = RGB_SLAVE_HEAD;
    slave.cmd = RGB_SLAVE_CMD_CTRL;
    slave.control.IdxMode = 10;
    slave.control.Speed = 10;
    slave.control.background = 0x11223344;
    slave.control.logo = 0x14986344;
    slave.control.keys[3] = 0xAABBCCDD;
    slave.control.normally = 66;
    slave.control.reverse = 77;
    slave.control.Grayscale = 0x4F;
    memset(buf, 0x00, sizeof(buf));
    memset(debuf, 0x00, sizeof(debuf));
    len1 = rgb_slave_encode(buf, &slave);
    len2 = rgb_slave_decode(buf, sizeof(buf), &deslave);
    len3 = rgb_slave_check(buf, sizeof(buf));
    rgb_slave_encode(debuf, &deslave);
    if(0==memcmp(buf, debuf, sizeof(buf))) printf("[%s--%d] Test OK len: %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
    else
    {
        printf("[%s--%d] len:  %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
        printf("[%s--%d] head: 0x%02X 0x%02X \r\n", __func__, __LINE__, slave.head, deslave.head);
    }
    memset(buf, 0x00, sizeof(buf));
    memset(debuf, 0x00, sizeof(debuf));
    len1 = rgb_slave_ctrl(buf, slave.control.IdxMode, slave.control.Speed, slave.control.reverse, slave.control.Grayscale, slave.control.background, slave.control.logo, slave.control.keys, slave.control.normally);
    len2 = rgb_slave_decode(buf, sizeof(buf), &deslave);
    len3 = rgb_slave_check(buf, sizeof(buf));
    len3 = rgb_slave_ctrl_decode(buf, sizeof(buf), &ctrl);;
    rgb_slave_encode(debuf, &deslave);
    if(0==memcmp(buf, debuf, sizeof(buf))) printf("[%s--%d] Test OK len: %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
    else
    {
        printf("[%s--%d] len:  %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
        printf("[%s--%d] head: 0x%02X 0x%02X \r\n", __func__, __LINE__, slave.head, deslave.head);
        printf("[%s--%d] cmd:  0x%02X 0x%02X \r\n", __func__, __LINE__, slave.cmd, deslave.cmd);
        printf("[%s--%d] IdxMode: 0x%02X 0x%02X \r\n", __func__, __LINE__, slave.control.IdxMode, deslave.control.IdxMode);
        printf("[%s--%d] Speed: 0x%02X 0x%02X \r\n", __func__, __LINE__, slave.control.Speed, deslave.control.Speed);
        printf("[%s--%d] background: 0x%02X 0x%02X \r\n", __func__, __LINE__, slave.control.background, deslave.control.background);
        printf("[%s--%d] keys: 0x%02X 0x%02X \r\n", __func__, __LINE__, slave.control.keys[3], deslave.control.keys[3]);
        printf("[%s--%d] normally: 0x%02X 0x%02X \r\n", __func__, __LINE__, slave.control.normally, deslave.control.normally);
        printf("[%s--%d] reverse: 0x%02X 0x%02X \r\n", __func__, __LINE__, slave.control.reverse, deslave.control.reverse);
    }
}
static void rgb_slave_test_udef(void)
{
    union RGB_Slave_t slave;
    union RGB_Slave_t deslave;
    uint8_t buf[512];
    uint8_t debuf[512];
    int len1,len2, len3;
    slave.head = RGB_SLAVE_HEAD;
    slave.cmd = RGB_SLAVE_CMD_UDEF;
    //slave.transfer.offset = 21;
    slave.transfer_rgb[11] = 0xCC;
    memset(buf, 0x00, sizeof(buf));
    memset(debuf, 0x00, sizeof(debuf));
    len1 = rgb_slave_encode(buf, &slave);
    len2 = rgb_slave_decode(buf, sizeof(buf), &deslave);
    len3 = rgb_slave_check(buf, sizeof(buf));
    rgb_slave_encode(debuf, &deslave);
    if(0==memcmp(buf, debuf, sizeof(buf))) printf("[%s--%d] Test OK len: %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
    else
    {
        printf("[%s--%d] len:  %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
        printf("[%s--%d] head: 0x%02X 0x%02X \r\n", __func__, __LINE__, slave.head, deslave.head);
    }
    memset(buf, 0x00, sizeof(buf));
    memset(debuf, 0x00, sizeof(debuf));
    len1 = rgb_slave_udef(buf/*, slave.transfer.offset*/, slave.transfer_rgb);
    len2 = rgb_slave_decode(buf, sizeof(buf), &deslave);
    len3 = rgb_slave_check(buf, sizeof(buf));
    rgb_slave_encode(debuf, &deslave);
    if(0==memcmp(buf, debuf, sizeof(buf))) printf("[%s--%d] Test OK len: %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
    else
    {
        printf("[%s--%d] len:  %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
        printf("[%s--%d] head: 0x%02X 0x%02X \r\n", __func__, __LINE__, slave.head, deslave.head);
    }
}
static void rgb_slave_test_record(void)
{
    union RGB_Slave_t slave;
    union RGB_Slave_t deslave;
    uint8_t buf[512];
    uint8_t debuf[512];
    int len1,len2, len3;
    slave.head = RGB_SLAVE_HEAD;
    slave.cmd = RGB_SLAVE_CMD_RECORD;
    //slave.transfer.offset = 42;
    slave.transfer_rgb[11] = 0xCC;
    memset(buf, 0x00, sizeof(buf));
    memset(debuf, 0x00, sizeof(debuf));
    len1 = rgb_slave_encode(buf, &slave);
    len2 = rgb_slave_decode(buf, sizeof(buf), &deslave);
    len3 = rgb_slave_check(buf, sizeof(buf));
    rgb_slave_encode(debuf, &deslave);
    if(0==memcmp(buf, debuf, sizeof(buf))) printf("[%s--%d] Test OK len: %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
    else
    {
        printf("[%s--%d] len:  %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
        printf("[%s--%d] head: 0x%02X 0x%02X \r\n", __func__, __LINE__, slave.head, deslave.head);
    }
    memset(buf, 0x00, sizeof(buf));
    memset(debuf, 0x00, sizeof(debuf));
    len1 = rgb_slave_record(buf, slave.transfer_rgb);
    len2 = rgb_slave_decode(buf, sizeof(buf), &deslave);
    len3 = rgb_slave_check(buf, sizeof(buf));
    rgb_slave_encode(debuf, &deslave);
    if(0==memcmp(buf, debuf, sizeof(buf))) printf("[%s--%d] Test OK len: %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
    else
    {
        printf("[%s--%d] len:  %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
        printf("[%s--%d] head: 0x%02X 0x%02X \r\n", __func__, __LINE__, slave.head, deslave.head);
    }
}
static void rgb_slave_test_direct_sub(const uint8_t sub)
{
    union RGB_Slave_t slave;
    union RGB_Slave_t deslave;
    uint8_t buf[512];
    uint8_t debuf[512];
    int len1,len2, len3;
    slave.head = RGB_SLAVE_HEAD;
    slave.cmd = sub;
    //slave.transfer.offset = 21;
    memset(buf, 0x00, sizeof(buf));
    memset(debuf, 0x00, sizeof(debuf));
    len1 = rgb_slave_encode(buf, &slave);
    len2 = rgb_slave_decode(buf, sizeof(buf), &deslave);
    len3 = rgb_slave_check(buf, sizeof(buf));
    rgb_slave_encode(debuf, &deslave);
    if(0==memcmp(buf, debuf, sizeof(buf))) printf("[%s--%d] Test OK len: %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
    else
    {
        printf("[%s--%d] len:  %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
        printf("[%s--%d] head: 0x%02X 0x%02X \r\n", __func__, __LINE__, slave.head, deslave.head);
    }
    memset(buf, 0x00, sizeof(buf));
    memset(debuf, 0x00, sizeof(debuf));
    rgb_slave_direct_sub(buf, sub, slave.direct_rgb);
    rgb_slave_decode(buf, sizeof(buf), &deslave);
    len3 = rgb_slave_check(buf, sizeof(buf));
    rgb_slave_encode(debuf, &deslave);
    if(0==memcmp(buf, debuf, sizeof(buf))) printf("[%s--%d] Test OK len: %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
    else
    {
        printf("[%s--%d] len:  %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
        printf("[%s--%d] head: 0x%02X 0x%02X \r\n", __func__, __LINE__, slave.head, deslave.head);
    }
}
static void rgb_slave_test_direct(void)
{
    union RGB_Slave_t slave;
    union RGB_Slave_t deslave;
    uint8_t buf[512];
    uint8_t debuf[512];
    int len1,len2, len3;
    slave.head = RGB_SLAVE_HEAD;
    slave.cmd = RGB_SLAVE_CMD_DIRECT;
    //slave.transfer.offset = 21;
    memset(buf, 0x00, sizeof(buf));
    memset(debuf, 0x00, sizeof(debuf));
    len1 = rgb_slave_encode(buf, &slave);
    len2 = rgb_slave_decode(buf, sizeof(buf), &deslave);
    len3 = rgb_slave_check(buf, sizeof(buf));
    rgb_slave_encode(debuf, &deslave);
    if(0==memcmp(buf, debuf, sizeof(buf))) printf("[%s--%d] Test OK len: %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
    else
    {
        printf("[%s--%d] len:  %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
        printf("[%s--%d] head: 0x%02X 0x%02X \r\n", __func__, __LINE__, slave.head, deslave.head);
    }
    memset(buf, 0x00, sizeof(buf));
    memset(debuf, 0x00, sizeof(debuf));
    rgb_slave_direct(buf, slave.direct_rgb);
    rgb_slave_decode(buf, sizeof(buf), &deslave);
    len3 = rgb_slave_check(buf, sizeof(buf));
    rgb_slave_encode(debuf, &deslave);
    if(0==memcmp(buf, debuf, sizeof(buf))) printf("[%s--%d] Test OK len: %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
    else
    {
        printf("[%s--%d] len:  %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
        printf("[%s--%d] head: 0x%02X 0x%02X \r\n", __func__, __LINE__, slave.head, deslave.head);
    }
    rgb_slave_test_direct_sub(RGB_SLAVE_CMD_DIRECT1);
    rgb_slave_test_direct_sub(RGB_SLAVE_CMD_DIRECT2);
    rgb_slave_test_direct_sub(RGB_SLAVE_CMD_DIRECT3);
}
static void rgb_slave_test_read(void)
{
    union RGB_Slave_t slave;
    union RGB_Slave_t deslave;
    uint8_t buf[128];
    uint8_t debuf[128];
    int len1,len2, len3;
    slave.head = RGB_SLAVE_HEAD;
    slave.cmd = RGB_SLAVE_CMD_READ;
    //slave.read.offset = 33;
    memset(buf, 0x00, sizeof(buf));
    memset(debuf, 0x00, sizeof(debuf));
    len1 = rgb_slave_encode(buf, &slave);
    len2 = rgb_slave_decode(buf, sizeof(buf), &deslave);
    len3 = rgb_slave_check(buf, sizeof(buf));
    rgb_slave_encode(debuf, &deslave);
    if(0==memcmp(buf, debuf, sizeof(buf))) printf("[%s--%d] Test OK len: %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
    else
    {
        printf("[%s--%d] len:  %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
        printf("[%s--%d] head: 0x%02X 0x%02X \r\n", __func__, __LINE__, slave.head, deslave.head);
    }
    memset(buf, 0x00, sizeof(buf));
    memset(debuf, 0x00, sizeof(debuf));
    len1 = rgb_slave_read(buf/*, slave.read.offset*/);
    len2 = rgb_slave_decode(buf, sizeof(buf), &deslave);
    len3 = rgb_slave_check(buf, sizeof(buf));
    rgb_slave_encode(debuf, &deslave);
    if(0==memcmp(buf, debuf, sizeof(buf))) printf("[%s--%d] Test OK len: %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
    else
    {
        printf("[%s--%d] len:  %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
        printf("[%s--%d] head: 0x%02X 0x%02X \r\n", __func__, __LINE__, slave.head, deslave.head);
    }
}
static void rgb_slave_test_test(void)
{
    union RGB_Slave_t slave;
    union RGB_Slave_t deslave;
    struct RGB_Slave_test test;
    uint8_t buf[128];
    uint8_t debuf[128];
    int len1,len2, len3;
    slave.head = RGB_SLAVE_HEAD;
    slave.cmd = RGB_SLAVE_CMD_TEST;
    slave.test.col = 5;
    slave.test.row = 3;
    slave.test.IdxLogo = 0xFF;
    slave.test.reverseX = 0x55;
    slave.test.reverseY = 0x00;
    slave.test.masks[2] = 0x67893322;
    slave.test.color    = 0x44556633;
    memset(buf, 0x00, sizeof(buf));
    memset(debuf, 0x00, sizeof(debuf));
    len1 = rgb_slave_encode(buf, &slave);
    len2 = rgb_slave_decode(buf, sizeof(buf), &deslave);
    len3 = rgb_slave_check(buf, sizeof(buf));
    rgb_slave_encode(debuf, &deslave);
    if(0==memcmp(buf, debuf, sizeof(buf))) printf("[%s--%d] Test OK len: %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
    else
    {
        printf("[%s--%d] len:  %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
        printf("[%s--%d] head: 0x%02X 0x%02X \r\n", __func__, __LINE__, slave.head, deslave.head);
    }
    memset(buf, 0x00, sizeof(buf));
    memset(debuf, 0x00, sizeof(debuf));
    len1 = rgb_slave_test(buf, slave.test.col, slave.test.row, slave.test.IdxLogo, slave.test.reverseX, slave.test.reverseY, slave.test.masks, slave.test.color);
    len2 = rgb_slave_decode(buf, sizeof(buf), &deslave);
    len3 = rgb_slave_check(buf, sizeof(buf));
    len3 = rgb_slave_test_decode(buf, sizeof(buf), &test);
    rgb_slave_encode(debuf, &deslave);
    if(0==memcmp(buf, debuf, sizeof(buf))) printf("[%s--%d] Test OK len: %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
    else
    {
        printf("[%s--%d] len:  %2d %2d %2d\r\n", __func__, __LINE__, len1, len2, len3);
        printf("[%s--%d] head: 0x%02X 0x%02X \r\n", __func__, __LINE__, slave.head, deslave.head);
    }
}

void RGB_Slave_Test(void)
{
    rgb_slave_test_ack();
    rgb_slave_test_sleep();
    rgb_slave_test_init();
    rgb_slave_test_ctrl();
    rgb_slave_test_udef();
    rgb_slave_test_record();
    rgb_slave_test_direct();
    rgb_slave_test_read();
    rgb_slave_test_test();
    fflush(stdout);
}



